package com.a.learning_keep.base

import com.google.gson.Gson
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody

var Url = "http://10.100.41.91/part-time/api"
var token = ""

fun <T> get(url: String, c: Class<T>): T {
    val build = Request.Builder().url(Url + url).apply { if (token != "") addHeader("Authorization", token) }.build()
    return Gson().fromJson(OkHttpClient().newCall(build).execute().body?.string(), c)
}

fun <T> post(url: String, js: String, c: Class<T>): T {
    val create = RequestBody.create("application/json".toMediaTypeOrNull(), js)
    val build = Request.Builder().post(create).url(Url + url).apply { if (token != "") addHeader("Authorization", token) }.build()
    return Gson().fromJson(OkHttpClient().newCall(build).execute().body?.string(), c)
}


